package com.it.water.controller.VideoController;

import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;

import javax.swing.*;
import java.awt.image.BufferedImage;

public class RTSPVideoReceiver {

    public static void main(String[] args) throws Exception {
        String rtspUrl = "rtsp://admin:Admin123@211.64.41.139:554/live"; // 替换为实际的RTSP流地址

        FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(rtspUrl);
        grabber.start();

        JFrame window = new JFrame("RTSP Video Stream");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        while (true) {
            Frame frame = grabber.grabImage();
            if (frame != null) {
                showFrame(frame, window);
            } else {
                System.out.println("无法读取帧");
                break;
            }
        }

        grabber.stop();
    }

    public static void showFrame(Frame frame, JFrame window) {
        Java2DFrameConverter converter = new Java2DFrameConverter();
        BufferedImage image = converter.getBufferedImage(frame, 1.0);
        ImageIcon icon = new ImageIcon(image);
        JLabel label = new JLabel(icon);
        window.setContentPane(label);
        window.pack();
        window.setVisible(true);
    }
}
